<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/vue.js"></script>
</head>
<body>
  <div id="app">
    <h3>总价:¥{{total.toFixed(2)}}</h3>
    <ul>
      <li v-for="(p,i) of cart" :key="i">
        {{p.pid}} | {{p.pname}} | ¥{{p.price.toFixed(2)}} | {{p.count}} | 小计:¥{{(p.price*p.count).toFixed(2)}}
      </li>
    </ul>
    <h3>总价:¥{{total.toFixed(2)}}</h3>
  </div>
  <script>
    new Vue({
      el:"#app",
      data:{
        cart:[
          {pid:1, pname:"华为", price:5588, count:2},
          {pid:2, pname:"苹果", price:9588, count:1},
          {pid:3, pname:"小米", price:3588, count:3}
        ]
      },
      methods:{
        
      },
      computed:{
        total(){
          console.log(`自动调用了一次total()`)
          //定义变量保存临时汇总中，起始为0
          var result=0;
          //遍历购物车中每个商品对象
          for(var p of this.cart){
            //每遍历一个商品对象就计算当前商品的小计，并将小计累加到临时汇总值result变量中。
            result+=p.price*p.count;
          }
          //返回最终汇总值
          return result;
        }
      }
    })
  </script>
</body>
</html>
